'''
封装爬虫类
'''
import requests
from bs4 import BeautifulSoup


class Scrawler():
    def __init__(self):
        '''
        爬虫构造函数，接收url参数
        :param url:
        :return:
        '''
        self.headers = {
            "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36"
        }


#爬文本
    def getTargetText(self,url,selector):
        codes = requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes,'html.parser')
        return str([item.text for item in bs.select(selector=selector)])


#爬图片
    def getTargetImg(self,imgurl):
        imgc = requests.get(imgurl).content
        with open("work.jpg",'wb') as f:
            f.write(imgc)

if __name__== '__main__':
    url="https://www.baidu.com"
    s = Scrawler()
    imgurl = "https://imagecloud.thepaper.cn/thepaper/image/246/59/918.jpg"
    print(s.getTargetImg(imgurl))

